/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.util;
 

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 

public class DateUtil {
   

    public final static String DEFAULT_DATE_FORMAT = "yyyy年MM月dd日";
    public final static String DATETIME_FORMAT_HHMMSS = "yyyy年MM月dd日 HH时mm分ss秒";
    private static DateFormat DEFAULT_DF = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
    private static DateFormat DATETIME_DF = new SimpleDateFormat(DATETIME_FORMAT_HHMMSS);
    private static String default_timezone = "Asia/China";
    public static final String yyMMdd="yy-MM-dd";		//短日期格式
    public static final String yyyyMMdd="yyyy-MM-dd";	//长日期格式
    public static final String HHmmss="HH:mm:ss";		//时间格式
    public static final String yyyyMMddHHmmss
    							="yyyy-MM-dd HH:mm:ss";	//长日期时间格式
    public static final String yyMMddHHmmss
    							="yy-MM-dd HH:mm:ss";	//短日期时间格式

    public static Date parseToDate(String s, String style) {//字符串转换成日期时间
        SimpleDateFormat simpleDateFormat 
        					= new SimpleDateFormat();	//实例化日期格式化类
        simpleDateFormat.applyPattern(style);			//提交转换格式
        Date date = null;
        if(s==null||s.length()<8)						//字符串为空或长度小于8
            return null;								//无法转换，返回null值	
        try {
            date = simpleDateFormat.parse(s);			//进行转换
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String parseToString(String s, String style) {//格式化日期字符串
        SimpleDateFormat simpleDateFormat 
        					= new SimpleDateFormat();	//实例化日期格式化类
        simpleDateFormat.applyPattern(style);			//提交转换格式
        Date date = null;
        String str=null;
        if(s==null||s.length()<8)						//字符串为空或长度小于8
            return null;								//无法转换，返回null值
        try {
            date = simpleDateFormat.parse(s);			//将字符串转换成日期格式
            str=simpleDateFormat.format(date);			//将日期格式化为字符串
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String parseToString(Date date, String style) {//日期时间转换成字符串
        SimpleDateFormat simpleDateFormat 
        				= new SimpleDateFormat();		//实例化日期格式化类
        simpleDateFormat.applyPattern(style);			//提交转换格式
        String str=null;
        if(date==null)									//如果日期时间为null
            return null;								//返回null值
        str=simpleDateFormat.format(date);				//将日期格式化为字符串
        return str;
    }
    public static Date getDate() {
        TimeZone tz = TimeZone.getTimeZone(default_timezone);
        Calendar calendar = Calendar.getInstance(tz);
        return calendar.getTime();
    }

    public static String getCurrentTimestamps(){
    	int  h, m, s;
		Calendar cal = Calendar.getInstance();
		h = cal.get(Calendar.HOUR_OF_DAY);
		m = cal.get(Calendar.MINUTE);
		s = cal.get(Calendar.SECOND);
		return h+":"+m+":"+s;
    }
    
    public static Timestamp getCurrentTimestamp() {
        return new Timestamp(getDate().getTime());
    }

    public static String formatDate(Date date) {
        String dateTimeStr = "";
        try {
            dateTimeStr = DEFAULT_DF.format(date);
        } catch (Exception e) {
           
        }

        return dateTimeStr;
    }

    public static String formatDateTime(Date date) {
        String dateTimeStr = "";

        try {
            dateTimeStr = DATETIME_DF.format(date);
        } catch (Exception e) {
            
        }

        return dateTimeStr;
    }

    public static String formatDate(Date date, String format) {
        String dateTimeStr = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat(format);
            dateTimeStr = df.format(date);
        } catch (Exception e) {
             
        }

        return dateTimeStr;
    }

    public static Date parseDate(String dateStr) {
        Date date = null;

        try {
            date = DEFAULT_DF.parse(dateStr);
        } catch (Exception e) {
            
        }

        return date;
    }

    public static Date parseDateTime(String dateStr) {
        Date date = null;

        try {
            date = DATETIME_DF.parse(dateStr);
        } catch (Exception e) {
           
        }

        return date;
    }    

    public static Date parseDate(String str, String parsePatterns) {
        Date date = null;
        SimpleDateFormat parse;
        try {
            parse = new SimpleDateFormat(parsePatterns);
            date = parse.parse(str);
        } catch (ParseException e) {
          
        }
        return date;
    }

    public static Date getFullDate(Date date, Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        Calendar timeCal = Calendar.getInstance();
        timeCal.setTime(time);

        cal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
        cal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));

        return cal.getTime();
    }


    public static Date add(Date date, int field, int amount) {
        TimeZone tz = TimeZone.getTimeZone(default_timezone);
        Calendar cal = Calendar.getInstance(tz);
        cal.setTime(date);
        cal.add(field, amount);

        return cal.getTime();
    }

    public static Date getStartOfDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

        return getDate(year, month, day);
    }

    public static Date getEndOfDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        return cal.getTime();
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month - 1, day);
        return cal.getTime();
    }

    public static Date getStartDayOfMonth(Date aDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return DateUtil.getStartOfDay(cal.getTime());
    }

    public static Date getEndDayOfMonth(Date aDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtil.getEndOfDay(cal.getTime());
    }

    public static Date getStartDayOfWeek(Date aDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return cal.getTime();
    }

    public static Date getEndDayOfWeek(Date aDate) {
        return DateUtil.add(getStartDayOfWeek(aDate), Calendar.DATE, 6);
    }

    public static Date getStartDayOfWeek(Date aDate, int offset) {
        return DateUtil.add(getStartDayOfWeek(aDate), Calendar.WEEK_OF_YEAR, offset);
    }

    public static Date getEndDayOfWeek(Date aDate, int offset) {
        return DateUtil.add(getEndDayOfWeek(aDate), Calendar.WEEK_OF_YEAR, offset);
    }

    public static Date getStartDayOfMonth(Date aDate, int field, int offset) {
        return getStartDayOfMonth(DateUtil.add(aDate, field, offset));
    }

    public static Date getEndDayOfMonth(Date aDate, int field, int offset) {
        return getEndDayOfMonth(DateUtil.add(aDate, field, offset));
    }

    public static Date convertStringToDate(String input, String dateType, String firstLastInd){
        if(Constant.MONTH.equalsIgnoreCase(dateType)){
            String year=input.substring(0,4);
            String month=input.substring(4,6);
            if(Constant.FIRST.equalsIgnoreCase(firstLastInd)){
                 return getDate(Integer.parseInt(year),Integer.parseInt(month),1);
            }else{
                 return getEndDayOfMonth(getDate(Integer.parseInt(year),Integer.parseInt(month),1));
            }
        }else if(Constant.YEAR.equalsIgnoreCase(dateType)){
            if(Constant.FIRST.equalsIgnoreCase(firstLastInd)){
                 return getDate(Integer.parseInt(input),1,1);
            }else{
                 return getEndDayOfMonth(getDate(Integer.parseInt(input),12,31));
            }
        }
        return null;
    }

   public static void main(String[] args){
         System.out.println(convertStringToDate("201010",Constant.MONTH,Constant.FIRST));
        System.out.println(convertStringToDate("201010",Constant.MONTH,Constant.LAST));
        System.out.println(convertStringToDate("2010",Constant.YEAR,Constant.FIRST));
        System.out.println(convertStringToDate("2010",Constant.YEAR,Constant.LAST));
    } 
}
